#include "presetserver.h"
#include "logger.h"
#include <QSettings>
#include <QFile>

PresetServer::PresetServer(const QString &path) : m_filePath(path)
{
}

PresetServer::~PresetServer()
{
}

void PresetServer::initialize()
{
    QFile currentFile(m_filePath);
    if (!currentFile.exists())
    {// 配置不存在，使用默认值填充
        restoreDefault();
    }
    else
    {// 配置存在，填充不存在的项
        fillMissItems();
    }
}

void PresetServer::setData(int itemName, const QVariant &value)
{
    if (m_itemList.contains(itemName))
    {
        m_itemList[itemName].setData(value.toString());
        save(m_itemList[itemName]);
    }
}

bool PresetServer::getBoolData(int itemName)
{
    bool result = false;
    if (m_itemList.contains(itemName))
    {
        read(m_itemList[itemName]);
        result = m_itemList[itemName].getBoolData();
    }

    return result;
}

bool PresetServer::getBoolData(const QString &key)
{
    bool result = false;
    if(m_keyItemList.contains(key))
    {
        int item = m_keyItemList[key];
        result = getBoolData(item);
    }

    return result;
}

int PresetServer::getIntData(int itemName)
{
    int value = -1;
    if (m_itemList.contains(itemName))
    {
        read(m_itemList[itemName]);
        value = m_itemList[itemName].getIntData();
    }

    return value;
}

int PresetServer::getIntData(const QString &key)
{
    int value = false;
    if(m_keyItemList.contains(key))
    {
        int item = m_keyItemList[key];
        value = getIntData(item);
    }

    return value;
}

double PresetServer::getDoubleData(int itemName)
{
    int value = -1;
    if (m_itemList.contains(itemName))
    {
        read(m_itemList[itemName]);
        value = m_itemList[itemName].getDoubleData();
    }

    return value;
}

double PresetServer::getDoubleData(const QString &key)
{
    int value = false;
    if(m_keyItemList.contains(key))
    {
        int item = m_keyItemList[key];
        value = getDoubleData(item);
    }

    return value;
}

QString PresetServer::getStringData(int itemName)
{
    QString value;
    if (m_itemList.contains(itemName))
    {
        read(m_itemList[itemName]);
        value = m_itemList[itemName].getStringData();
    }

    return value;
}

QString PresetServer::getStringData(const QString &key)
{
    QString value;
    if (m_keyItemList.contains(key))
    {
        int item = m_keyItemList[key];
        value = getStringData(item);
    }

    return value;
}

int PresetServer::getIntDefault(int itemName)
{
    int defaultValue = -1;
    if (m_itemList.contains(itemName))
    {
        bool ret = false;
        PresetObject obj = m_itemList[itemName];
        defaultValue = obj.getDefaultData().toInt(&ret);
        if(!ret)
        {
            defaultValue = -1;
        }
    }

    return defaultValue;
}

double PresetServer::getDoubleDefault(int itemName)
{
    double defaultValue = 0.0;
    if (m_itemList.contains(itemName))
    {
        bool ret = false;
        PresetObject obj = m_itemList[itemName];
        defaultValue = obj.getDefaultData().toDouble(&ret);
        if(!ret)
        {
            defaultValue = 0.0;
        }
    }

    return defaultValue;
}

QString PresetServer::getStringDefault(int itemName)
{
    QString defaultValue = "";
    if (m_itemList.contains(itemName))
    {
        PresetObject obj = m_itemList[itemName];
        defaultValue = obj.getDefaultData();
    }

    return defaultValue;
}

QStringList PresetServer::getKeyList()
{
    QStringList keylist;
    QMap<QString, int>::ConstIterator its;
    for (its = m_keyItemList.begin(); its != m_keyItemList.end(); ++its)
    {
        QString keys = its.key();
        keylist.append(keys);
    }

    return keylist;
}

void PresetServer::restoreDefault(int itemName)
{
    QMap<int, PresetObject>::iterator itea = m_itemList.find(itemName);
    if (itea != m_itemList.end())
    {
        itea->restoreDefaultValue();
        save(*itea);
    }
}

void PresetServer::registerItem(int itemName, const QString &keyName, const QString &defaultValue)
{
    if (m_itemList.contains(itemName))
    {
        Q_ASSERT(false);
    }

    for (QMap<int, PresetObject>::iterator itea = m_itemList.begin(); itea != m_itemList.end(); ++itea)
    {
        if (itea->getKeyName() == keyName)
        {
            Q_ASSERT(false);
        }
    }

    PresetObject presetObject(itemName, keyName, defaultValue);
    m_itemList.insert(itemName, presetObject);
    m_keyItemList.insert(keyName, itemName);
}

void PresetServer::fillMissItems()
{
    QSettings iniAccess(m_filePath, QSettings::IniFormat);
    QStringList keys = iniAccess.allKeys();
    for (QMap<int, PresetObject>::iterator itea = m_itemList.begin(); itea != m_itemList.end(); ++itea)
    {
        const QString &key = itea->getKeyName();
        if (!keys.contains(key))
        {
            iniAccess.setValue(key, itea->getStringData());
            iniAccess.sync();
        }
    }
}

void PresetServer::restoreDefault()
{
    QSettings iniAccess(m_filePath, QSettings::IniFormat);
    for (QMap<int, PresetObject>::iterator itea = m_itemList.begin(); itea != m_itemList.end(); ++itea)
    {
        itea->restoreDefaultValue();
        const QString &key = itea->getKeyName();
        iniAccess.setValue(key, itea->getStringData());
        iniAccess.sync();
    }
}

void PresetServer::read(PresetObject &presetObject)
{
    QSettings iniAccess(m_filePath, QSettings::IniFormat);
    if (iniAccess.allKeys().contains(presetObject.getKeyName()))
    {
        QVariant var = iniAccess.value(presetObject.getKeyName());
        presetObject.setData(var.toString());
    }
}

void PresetServer::save(const PresetObject &presetObject)
{
    QString key = presetObject.getKeyName();
    QSettings iniAccess(m_filePath, QSettings::IniFormat);
    QString oriValue = iniAccess.value(key).toString();
    if (oriValue != presetObject.getStringData())
    {
        sysDebug((QString("%1: %2 \"%3\"->\"%4\"").arg("Modify Preset").arg(key).arg(oriValue).arg(presetObject.getStringData())).toStdString().data());
        iniAccess.setValue(key, presetObject.getStringData());
        iniAccess.sync();
    }
}
